package popcorn.ticket.serviceimpl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import popcorn.ticket.dao.OrdersMapper;
import popcorn.ticket.dao.ScheduleMapper;
import popcorn.ticket.entity.Grades;
import popcorn.ticket.entity.MovieHallType;
import popcorn.ticket.entity.MovieHalls;
import popcorn.ticket.entity.Movies;
import popcorn.ticket.entity.Orders;
import popcorn.ticket.entity.Schedule;
import popcorn.ticket.entity.ScheduleExample;
import popcorn.ticket.service.ScheduleService;
import popcorn.ticket.tool.GetEndTimeTools;
import popcorn.ticket.tool.MH1;
import popcorn.ticket.tool.MH2;
import popcorn.ticket.tool.MovieSche;
import popcorn.ticket.tool.ScheInfo;
import popcorn.ticket.tool.SeatInfo;

/**
 * 排期 服务层 实现类
 * 
 * @author 傅洁敏
 *
 */
@Service
public class ScheduleServiceImpl implements ScheduleService {
	@Autowired
	ScheduleMapper sm;
	@Autowired
	OrdersMapper om;

	/**
	 * 获取影厅信息
	 */
	@Override
	public List<MH1> getMH1() {
		// TODO Auto-generated method stub
		return sm.getMH1();
	}

	/**
	 * 获取电影信息
	 */
	@Override
	public List<MH2> getMH2() {
		// TODO Auto-generated method stub
		return sm.getMH2();
	}

	/**
	 * 获取排期的格式对象
	 */
	@Override
	public List<MovieSche> getSchedule() {
		ScheduleExample se = new ScheduleExample();
		List<Schedule> list = sm.selectByExample(se);
		if (list == null) {
			return null;
		} else {
			List<MovieSche> list1 = new ArrayList<MovieSche>();
			for (Schedule s : list) {
				// 结束时间
				Date end_date1 = GetEndTimeTools.getEndDate(s.getPlaytime(), s.getMovies().getDuration());
				SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String end_date = simple.format(end_date1);
				// 开始时间
				String start_date = simple.format(s.getPlaytime());
				// 创建格式对象
				MovieSche ms = new MovieSche(start_date, end_date, s.getMovies().getMoviename(), s.getScheduleid(),
						s.getHall().getHallid());
				list1.add(ms);
			}
			return list1;
		}
	}

	/**
	 * 添加排期
	 */
	@Override
	public int addSchedule(MovieSche ms) {
		// 通过电影名称找id
		int movieid = sm.getIdByName(ms.getText());
		MovieHalls mh = new MovieHalls();
		mh.setHallid(ms.getSection_id());

		Movies m = new Movies();
		m.setMovieid(movieid);

		Schedule s = new Schedule();
		s.setMovies(m);
		s.setHall(mh);
		s.setSchedulecondition(1);

		// string(2020-06-19T04:55:00.000 UTC)-->Date(Fri Jun 19 12:55:00 CST
		// 2020)-->string(2020-06-19 12:55:00)
		String start_date = ms.getStart_date();
		String dateStr = start_date.replace("Z", " UTC");
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
		String start_datestr = null;
		try {
			Date date1 = df.parse(dateStr);
			SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
			Date date = df1.parse(date1.toString());
			SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			start_datestr = df2.format(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ms.setStart_date(start_datestr);

		SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			s.setPlaytime(simple.parse(ms.getStart_date()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 添加
		int n = sm.insert(s);
		return n;
	}

	/**
	 * 修改
	 */
	@Override
	public int updSche(MovieSche ms) {
		// 通过电影名称找id
		int movieid = sm.getIdByName(ms.getText());
		MovieHalls mh = new MovieHalls();
		mh.setHallid(ms.getSection_id());

		Movies m = new Movies();
		m.setMovieid(movieid);

		Schedule s = new Schedule();
		s.setMovies(m);
		s.setHall(mh);
		// 排期id
		s.setScheduleid(ms.getAuto());
		s.setSchedulecondition(1);

		// string(2020-06-19T04:55:00.000 UTC)-->Date(Fri Jun 19 12:55:00 CST
		// 2020)-->string(2020-06-19 12:55:00)
		String start_date = ms.getStart_date();
		String dateStr = start_date.replace("Z", " UTC");
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
		String start_datestr = null;
		try {
			Date date1 = df.parse(dateStr);
			SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
			Date date = df1.parse(date1.toString());
			SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			start_datestr = df2.format(date);
			ms.setStart_date(start_datestr);
			s.setPlaytime(df2.parse(ms.getStart_date()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		// 修改
		int n = sm.updateByPrimaryKeySelective(s);
		return n;
	}

	/**
	 * 删除
	 */
	@Override
	public int delSche(int scheduleid) {
		int n = sm.deleteByPrimaryKey(scheduleid);
		return n;
	}

	/**
	 * 打包前台所需排期的数据
	 */
	@Override
	public ScheInfo getInfof(String time, int movieid) {
		List<MovieHalls> listmh = sm.getHallIdName(time, movieid);
		List<Schedule> lists = sm.getTime(time, movieid);
		ScheInfo si = new ScheInfo(listmh, lists);
		return si;
	}

	/**
	 * 打包选座页面所需的数据
	 */
	@Override
	public SeatInfo getInfoj(int movieid, int scheduleid) {
		// 票价
		List<Movies> m = sm.getPrice(movieid);
		// 影厅布局
		List<MovieHallType> mh = sm.getGalleryful(scheduleid);
		// 已选座
		List<String> s = sm.getSeat(scheduleid);
		List<String> ss = new ArrayList<String>();
		for (String s1 : s) {
			String arr[] = s1.split(",");
			for (int i = 0; i < arr.length; i++) {
				ss.add(arr[i]);
			}
		}
		SeatInfo si = new SeatInfo(m, mh, ss);
		return si;
	}

	/**
	 * 通过userid获取折扣
	 */
	@Override
	public Grades getDiscoutLevel(int userid) {
		// TODO Auto-generated method stub
		return sm.getDiscoutLevel(userid);
	}

	/**
	 * 增加订单
	 */
	@Override
	public int addOrders(Orders o) {
		// TODO Auto-generated method stub
		return om.insertSelective(o);
	}
	
	/**
	 * 查询排期信息
	 */
	@Override
	public Schedule selScheduleByid(int scheduleid) {
		
		return sm.selectByPrimaryKey(scheduleid);
	}

}
